package com.maxlen.order.listener;

import com.maxlen.order.entity.OrderEntity;
import com.maxlen.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "order.order.release.queue")
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity orderEntity, @Headers Message headers, Channel channel) throws IOException {
        System.out.println(orderEntity.getOrderSn());
        System.out.println("开始取消订单.....");
        try{
            orderService.closeOrder(orderEntity);
            channel.basicAck(headers.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            System.out.println(e);
            channel.basicReject(headers.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
